package com.hlingsoft.cms.action.manage;

import com.hlingsoft.cms.dao.GoodsDao;
import com.hlingsoft.cms.dao.SizeWithPriceDao;
import com.hlingsoft.cms.entity.*;
import com.hlingsoft.cms.entity.vo.JsonVo;
import com.hlingsoft.cms.entity.vo.PageVo;

import com.hlingsoft.cms.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by selbylei on 2015/6/12.
 */
@Controller
@RequestMapping("manage/price")
public class ManagePriceAction extends ManageBaseAction {

    @Autowired
    private SizeWithPriceDao sizeWithPriceDao;

    @Autowired
    private GoodsDao goodsDao;

    @RequestMapping(value = "/list.htm")
    public String index(
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<SizeWithPrice> pageVo = new PageVo<SizeWithPrice>(pageNum);
        pageVo.setRows(20);
        pageVo.basePath = "/manage/price/list.htm";
        List<SizeWithPrice> sizeWithPriceList = sizeWithPriceDao.getSizeWithPriceList();
        List<SizeWithPrice> result= new ArrayList<SizeWithPrice>();
        for(SizeWithPrice sizeWithPrice:sizeWithPriceList){
            Goods goods=goodsDao.getGoodsById(sizeWithPrice.getGoodsId());
            if (goods!=null){
                sizeWithPrice.setGoodsName(goods.getName());
                result.add(sizeWithPrice);
            }
        }

        pageVo.setList(Common.fenYe(result,pageVo.getOffset(),pageVo.getRows()));
        pageVo.setCount(result.size());
        modelMap.put("p", pageNum);
        modelMap.put("pageVo", pageVo);
        return "manage/price/list";
    }


    /**
     * 添加
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/add.htm", method = RequestMethod.GET)
    public String add(
            ModelMap modelMap) {
        return "manage/price/add";
    }


    @ResponseBody
    @RequestMapping(value = "/add.json")
    public JsonVo<String> add(
            @RequestParam(value = "goodsId") long goodsId,
            @RequestParam(value = "size") String size,
            @RequestParam(value = "price") Integer price,
            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {
            Date now = new Date();
            SizeWithPrice sizeWithPrice = new SizeWithPrice();
            sizeWithPrice.setGoodsId(goodsId);
            sizeWithPrice.setSize(size);
            sizeWithPrice.setPrice(price.doubleValue());
            sizeWithPrice.setCreateTime(now);
            sizeWithPrice.setUpdateTime(now);
            sizeWithPriceDao.addSizeWithPrice(sizeWithPrice);
            json.setResult(true);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            json.setResult(false);
            return json;
        }
    }


    /**
     * 编辑
     * @param priceId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/edit.htm")
    public String edit(
            @RequestParam(value = "priceId") long priceId,
            ModelMap modelMap) {
        SizeWithPrice sizeWithPrice  = sizeWithPriceDao.getSizeWithPriceById(priceId);
        Goods goods=goodsDao.getGoodsById(sizeWithPrice.getGoodsId());
        if (goods!=null){
            sizeWithPrice.setGoodsName(goods.getName());
        }
        modelMap.put("price",sizeWithPrice);
        return "manage/price/edit";
    }


    @ResponseBody
    @RequestMapping(value = "/edit.json")
    public JsonVo<String> edit(
            @RequestParam(value = "priceId") long priceId,
            @RequestParam(value = "goodsId") long goodsId,
            @RequestParam(value = "size") String size,
            @RequestParam(value = "price") Double price,
            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {
            Date now = new Date();
            SizeWithPrice sizeWithPrice = sizeWithPriceDao.getSizeWithPriceById(priceId);
            sizeWithPrice.setGoodsId(goodsId);
            if (size!=null){
                sizeWithPrice.setSize(size);
            }  if (price!=null){
                sizeWithPrice.setPrice(price.doubleValue());
            }
            sizeWithPrice.setUpdateTime(now);
            sizeWithPriceDao.updateSizeWithPrice(sizeWithPrice);
            json.setResult(true);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            json.setResult(false);
            return json;
        }
    }



    /**
     * 删除价格
     * @param priceId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "priceId") long priceId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        sizeWithPriceDao.deletePrice(priceId);
        json.setResult(true);
        return json;
    }

}
